package com.iov.car.controller;

import java.util.List;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.alibaba.fastjson.JSONArray;
import com.iov.car.domain.TbCarinfoVo;
import com.iov.car.domain.TbDictionary;
import com.iov.car.domain.TypeVo;
import com.iov.car.service.ITbDictionaryService;
import com.iov.car.service.ITbFenceService;
import io.swagger.models.auth.In;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.iov.car.domain.TbCarinfo;
import com.iov.car.service.ITbCarinfoService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 车辆信息Controller
 * 
 * @author ruoyi
 * @date 2023-11-13
 */
@RestController
@RequestMapping("/carinfo")
public class TbCarinfoController extends BaseController
{
    @Autowired
    private ITbCarinfoService tbCarinfoService;
    @Autowired
    private ITbDictionaryService tbDictionaryService;

    @Autowired
    private ITbFenceService tbFenceService;

    /**
     * 查询车辆信息列表
     */
    @RequiresPermissions("car:carinfo:list")
    @GetMapping("/list")
    public TableDataInfo list(TbCarinfo tbCarinfo)
    {
        startPage();
        List<TbCarinfo> list = tbCarinfoService.selectTbCarinfoList(tbCarinfo);
        List<TbCarinfoVo> collect = list.stream().map(car -> {
            TbCarinfoVo tbCarinfoVo = new TbCarinfoVo(  );
            BeanUtils.copyProperties(car, tbCarinfoVo);
            if (car.getCarType() != null) {
                List<Integer> integers = JSONArray.parseArray(car.getCarType(), Integer.class);
                String typeName = "";
                for (Integer id : integers) {
                    TbDictionary tbDictionary = tbDictionaryService.getCarType(id);
                    typeName += "/"+tbDictionary.getName();
                }
                tbCarinfoVo.setCarName(typeName.substring(1));
            }
            return tbCarinfoVo;
        } ).collect(Collectors.toList());
        return getDataTable(collect);
    }

    /**
     * 获取车辆类型
     * @return
     */
    @RequiresPermissions("car:carinfo:getCarType")
    @GetMapping("/getCarType")
    public TableDataInfo getCarType() {
        List<TbDictionary> list = tbDictionaryService.getList();
        List<TypeVo> collect = list.stream().filter(item -> {
            return item.getOrderId() == 0;
        }).map(a -> {
            TypeVo typeVo = new TypeVo();
            typeVo.setValue(a.getId() + "");
            typeVo.setLabel(a.getName());
            List<TypeVo> subtype = subtype(a.getId(), list);
            typeVo.setChildren(subtype.size() > 0 ? subtype : null);
            return typeVo;
        }).collect(Collectors.toList());
        collect.forEach(System.out::println);
        return getDataTable(collect);
    }

    /**
     * 获取电机厂商
     * @return
     */
    private List<TypeVo> subtype(Long id, List<TbDictionary> list) {
        List<TypeVo> collect = list.stream().filter(a -> {
            return a.getOrderId().equals(id);
        }).map(item -> {
            TypeVo typeVo1 = new TypeVo();
            typeVo1.setValue(item.getId() + "");
            typeVo1.setLabel(item.getName());
            List<TypeVo> subtype = subtype(item.getId(), list);
            typeVo1.setChildren(subtype.size() > 0 ? subtype : null);
            return typeVo1;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 获取电池厂商
     * @param id
     * @return
     */
    @RequiresPermissions("car:carinfo:getCarMachine")
    @GetMapping("/getCarMachine/{id}")
    public TableDataInfo getCarMachine(@PathVariable Integer id){
        List<TbDictionary> list=tbDictionaryService.getCarMachine(id);
        return getDataTable(list);
    }
    @RequiresPermissions("car:carinfo:getCarBattery")
    @GetMapping("/getCarBattery/{id}")
    public TableDataInfo getCarBattery(@PathVariable Integer id){
        List<TbDictionary> list=tbDictionaryService.getCarBattery(id);
        return getDataTable(list);
    }

    @RequiresPermissions("car:carinfo:getFence")
    @GetMapping("/getFence")
    public TableDataInfo getFence(){
        List<TbDictionary> list=tbFenceService.getFence();
        return getDataTable(list);
    }
    /**
     * 导出车辆信息列表
     */
    @RequiresPermissions("car:carinfo:export")
    @Log(title = "车辆信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbCarinfo tbCarinfo)
    {
        List<TbCarinfo> list = tbCarinfoService.selectTbCarinfoList(tbCarinfo);
        ExcelUtil<TbCarinfo> util = new ExcelUtil<TbCarinfo>(TbCarinfo.class);
        util.exportExcel(response, list, "车辆信息数据");
    }

    /**
     * 获取车辆信息详细信息
     */
    @RequiresPermissions("car:carinfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbCarinfoService.selectTbCarinfoById(id));
    }

    /**
     * 新增车辆信息
     */
    @RequiresPermissions("car:carinfo:add")
    @Log(title = "车辆信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody TbCarinfo tbCarinfo)
    {

        tbCarinfo.setIsStatus("1");
        return toAjax(tbCarinfoService.insertTbCarinfo(tbCarinfo));
    }

    /**
     * 修改车辆信息
     */
    @RequiresPermissions("car:carinfo:edit")
    @Log(title = "车辆信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbCarinfo tbCarinfo)
    {
        return toAjax(tbCarinfoService.updateTbCarinfo(tbCarinfo));
    }

    /**
     * 删除车辆信息
     */
    @RequiresPermissions("car:carinfo:remove")
    @Log(title = "车辆信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {

        return toAjax(tbCarinfoService.deleteTbCarinfoByIds(ids));
    }
    @RequiresPermissions("car:carinfo:getCarVo")
    @GetMapping("getCarVo")
    public List<TbCarinfoVo> getCarVo(TbCarinfo tbCarinfo){
        List<TbCarinfo> list = tbCarinfoService.selectTbCarinfoList(tbCarinfo);
        List<TbCarinfoVo> collect = list.stream().map(car -> {
            TbCarinfoVo tbCarinfoVo = new TbCarinfoVo();
            BeanUtils.copyProperties(car, tbCarinfoVo);
            if (car.getCarType()!= null) {
                List<Integer> integers = JSONArray.parseArray(car.getCarType(), Integer.class);
                String typeName = "";
                for (Integer id : integers) {
                    TbDictionary tbDictionary = tbDictionaryService.getCarType(id);
                    typeName += "/"+tbDictionary.getName();
                }
                tbCarinfoVo.setCarName(typeName.substring(1));
            }
            return tbCarinfoVo;
        } ).collect(Collectors.toList());
        return collect;
    }
}
